package main

import (
	"oa-rest/common"
	"oa-rest/config"
	"oa-rest/models/lnk"

	log "github.com/sirupsen/logrus"
	"xorm.io/xorm"
)

func main() {
	config.InitEnv()
	db, err := common.ConnectDb()
	if err != nil {
		println(err)
	}
	defer db.Close()
	_, err = db.Exec("drop table if exists lnk")
	_, err = db.Exec("drop table if exists user_lnk")
	_, err = db.Exec("drop table if exists org_lnk")
	if err != nil {
		log.Fatal(err)
	}

	err = db.Sync2(
		new(lnk.Lnk),
		new(lnk.UserLnk),
		new(lnk.OrgLnk),
	)
	if err != nil {
		log.Fatal(err)
	}
	initLnk(db)
	initOrgLnk(db)
}

func initLnk(db *xorm.Engine) {
	_, err := db.Insert([]*lnk.Lnk{
		{Id: 1, Label: "智能问答", IconName: "ChatDotRound", Type: "success", ClassName: "action-btn-large", Link: "/ai-qa", Order: 1},
		{Id: 2, Label: "公共文档", IconName: "FolderOpened", Type: "warning", ClassName: "action-btn-large", Link: "/public-docs", Order: 2},
		{Id: 3, Label: "文化园地", IconName: "Reading", Type: "primary", ClassName: "action-btn-large", Link: "/culture-garden", Order: 3},
		{Id: 4, Label: "文件共享", IconName: "UserFilled", Type: "success", ClassName: "action-btn-large", Link: "openFileShareDialog", Order: 4},
		{Id: 5, Label: "发起会议", IconName: "ChatLineRound", Type: "warning", ClassName: "action-btn-large", Link: "openCallMeetingDialog", Order: 5},
		{Id: 6, Label: "流程中心", IconName: "ChatLineSquare", Type: "warning", ClassName: "action-btn-large", Link: "/task-center", Order: 6},
		{Id: 7, Label: "发起流程", IconName: "EditPen", Type: "primary", ClassName: "action-btn-large", Link: "/new-proposal", Order: 7},
		{Id: 8, Label: "发起公文", IconName: "DocumentAdd", Type: "primary", ClassName: "action-btn-large", Link: "/new-document", Order: 8},
		{Id: 9, Label: "发文办理", IconName: "ArrowRightBold", Type: "danger", ClassName: "action-btn-large", Link: "/send-doc", Order: 9},
		{Id: 10, Label: "收文登记", IconName: "ArrowLeftBold", Type: "warning", ClassName: "action-btn-large", Link: "/receive-doc", Order: 10},
	})
	if err != nil {
		log.Fatal(err)
	}
}

// 除了会长和秘书，其他看到的都一样
func initOrgLnk(db *xorm.Engine) {
	orgIds1 := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}
	orgIds2 := []int64{2, 3}
	orgIds3 := []int64{4, 5, 6, 7, 8, 9}

	orgLnks := make([]lnk.OrgLnk, 0)
	// 先加同样的
	for _, orgId := range orgIds1 {
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 1})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 2})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 3})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 4})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 5})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 6})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 7})
	}
	orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: 1, LnkId: 1})

	for _, orgId := range orgIds2 {
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 9})
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 10})
	}

	for _, orgId := range orgIds3 {
		orgLnks = append(orgLnks, lnk.OrgLnk{OrgId: orgId, LnkId: 8})
	}

	_, err := db.Insert(&orgLnks)
	if err != nil {
		log.Fatal(err)
	}
}
